#include "EntityManager.h"
#include "BaseGameEntity.h"
#include "Logic/Role.h"
#include "Render/Visual.h"
#include "Render/Animate/Animate.h"

EntityManager* EntityManager::Instance()
{
	static EntityManager instance;

	return &instance;
}


BaseGameEntity* EntityManager::GetEntityFromID(int id)const
{

	EntityMap::const_iterator ent = m_EntityMap.find(id);

	assert ( (ent !=  m_EntityMap.end()) && "<EntityManager::GetEntityFromID>: invalid ID");

	return ent->second;
}


void EntityManager::RemoveEntity(BaseGameEntity* pEntity)
{    
	m_EntityMap.erase(m_EntityMap.find(pEntity->GetID()));

} 

void EntityManager::RegisterEntity(BaseGameEntity* NewEntity)
{
	m_EntityMap.insert(std::make_pair(NewEntity->GetID(), NewEntity));
}

EntityMap::const_iterator EntityManager::Begin()
{
	return m_EntityMap.begin();
}

EntityMap::const_iterator EntityManager::End()
{
	return m_EntityMap.end();
}


BaseGameEntity* EntityManager::CreateEntity(const char* type)
{
	if (!strcmpi(type, "role"))
	{
		Visual* pVisual = new Visual(ENTITY_ROLE);
		Animate* pBody = new Animate("data/body");
		Animate* pHead = new Animate("data/head", pBody);
		pVisual->AddAnimate(pBody);
		pVisual->AddAnimate(pHead);
		RenderData data;
		data.point.x = 2*32 + 16;
		data.point.y = 5*32 + 16;
		data.state   = 0;
		data.direction = 0; //down
		data.time = 25;     //default OneActTime, no influence anyway.

		pVisual->SetRenderData(data);
	
		Role* pRet = new Role(pVisual);
		RegisterEntity(pRet);
		return pRet;
	}
	else if (!strcmpi(type, "enemy"))
	{
		Visual* pVisual = new Visual(ENTITY_ROLE);
		Animate* pBody = new Animate("data/body");
		Animate* pHead = new Animate("data/head", pBody);
		pVisual->AddAnimate(pBody);
		pVisual->AddAnimate(pHead);
		RenderData data;
		data.point.x = 14*32 + 16;
		data.point.y = 12*32 + 16;
		data.state   = 0;
		data.direction = 0; //down
		data.time = 25;     //default OneActTime, no influence anyway.

		pVisual->SetRenderData(data);

		Role* pRet = new Role(pVisual);
		pRet->SetPos(Vector2Di(14,12));
		RegisterEntity(pRet);
		return pRet;
	}
	else
	{
		assert(0&&"CreateEntity():Unknown type!");
	}
	return NULL;
}
